//
//  TimeLineView.m
//  SecondPage
//
//  Created by xiong qi on 13-4-19.
//  Copyright (c) 2013年 winston. All rights reserved.
//

#import "TimeLineView.h"
#define AnimationDuration 0.3
#define MaxShowCount      6
#define ItemSpace         10

@implementation TimeLineView
@synthesize favoriteConfig;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        lineView = [[UIImageView alloc] initWithFrame:CGRectMake(0, (frame.size.height-8)/2+5, frame.size.width, 8)];
        lineView.image = [UIImage imageNamed:@"AllAuto_Config_TimeLine_BG"];
        [self addSubview:lineView];
        [lineView release];
        
        scrollview = [[UIScrollView alloc] initWithFrame:self.bounds];
        [self addSubview:scrollview];
        [scrollview release];
        scrollview.showsHorizontalScrollIndicator = NO;
        scrollview.showsVerticalScrollIndicator   = NO;
        scrollview.scrollEnabled = NO;
        
        sliderView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, 18, 20)];
        sliderView.image = [UIImage imageNamed:@"AllAuto_Config_TimeLine_Slider"];
        [scrollview addSubview:sliderView];
        [sliderView release];
        
    }
    return self;
}

-(void)RefreshByFavorite:(NSArray *)array
{
    
    NSLog(@"%@",array);
    
    if (array == nil) {
        return;
    }
    self.favoriteConfig = array;
    int count = [favoriteConfig count];
    if (count>MaxShowCount) {
        itemWidth = scrollview.frame.size.width/MaxShowCount;
    }
    else
    {
        itemWidth = scrollview.frame.size.width/count;
    }
    int viewwidth = itemWidth-ItemSpace*2;
    
    for (int i=0; i<count; i++) {
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(i*itemWidth+ItemSpace, 10, viewwidth, 25)];
        label.tag = i+100;
        [scrollview addSubview:label];
        [label setBackgroundColor:[UIColor clearColor]];
        [label release];
        [label setTextColor:[UIColor colorWithRed:0.4 green:0.4 blue:0.4 alpha:1.0]];
        label.font = [UIFont systemFontOfSize:12];
        label.textAlignment = NSTextAlignmentCenter;
        
        id obj = [favoriteConfig objectAtIndex:i];
        if ([obj isKindOfClass:[NSDictionary class]]) {
            NSDictionary * dic = [favoriteConfig objectAtIndex:i];
            label.text = [dic objectForKey:@"Title"];
        }
        else
        {
            label.text = [favoriteConfig objectAtIndex:i];
        }
        
        
        if (i == 0) {
            sliderView.center = CGPointMake(label.center.x, lineView.center.y);
        }
    }
    
    scrollview.contentSize = CGSizeMake(itemWidth * count, self.frame.size.height);
}

-(void)SetShowIndex:(int)index
{
    int offsetx = scrollview.contentOffset.x;
    int count = [favoriteConfig count];
    int pageindex = index/MaxShowCount;
    
    if (index >0 && (index%MaxShowCount == 0)) {
        
        
        int different = count-index;
        
        if (different>=MaxShowCount) {
            offsetx = pageindex*self.frame.size.width;
        }
        else if(different>0 && different<MaxShowCount)
        {
            offsetx = pageindex*self.frame.size.width-(MaxShowCount - different)*itemWidth;
            
        }
    }
    else if((index+1)%MaxShowCount == 0)
    {
        offsetx = pageindex*self.frame.size.width;
    }
    
    
    
//    int pageindex = index/MaxShowCount;
//    NSLog(@"%d,%f",index*itemWidth,(offsetx+scrollview.frame.size.width));
//    //超出右边界,需要滚动
//    if (index*itemWidth>=offsetx+scrollview.frame.size.width) {
//        
//        int different = count-(index+1);
//        
//        if (different>=MaxShowCount) {
//            offsetx = pageindex*self.frame.size.width;
//        }
//        else if(different>0 && different<MaxShowCount)
//        {
//            offsetx = pageindex*self.frame.size.width-(MaxShowCount - different)*itemWidth;
//            
//        }
//        
//    }
//    else if(((index+1)*itemWidth-ItemSpace)<=offsetx)
//    {
//        offsetx = pageindex*self.frame.size.width;
//    }
    
    
    
    
    for (int i=0; i<count; i++) {
        UILabel * label = (UILabel *)[self viewWithTag:i+100];
        if (i == index) {
            label.font = [UIFont boldSystemFontOfSize:16];
            [label setTextColor:[UIColor colorWithRed:1 green:0 blue:0 alpha:1.0]];
            [UIView animateWithDuration:AnimationDuration
                             animations:^{
                                 
                                 sliderView.center = CGPointMake(label.center.x, lineView.center.y);
                                 scrollview.contentOffset = CGPointMake(offsetx, 0);
                                 
                             }
                             completion:^(BOOL finished){
                                 
                                 
                             }];
            
        }
        else
        {
            label.font = [UIFont systemFontOfSize:12];
            [label setTextColor:[UIColor colorWithRed:0.4 green:0.4 blue:0.4 alpha:1.0]];
        }
    }
    
}

-(void)dealloc
{
    self.favoriteConfig = nil;
    [super dealloc];
}

@end
